//
//  ViewController.swift
//  AlamofireUseDemo
//
//  Created by 李煜 on 2023/3/17.
//

import UIKit
import Alamofire
class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
//    1.请求
//    AF.request("https://httpbin.org/get").response { response in
//      debugPrint(response)
//    }

//    2.参数
//    let login = Login(email: "aaa", password: "bbb")
//    AF.request("https://httpbin.org/post",
//               method: .post,
//               parameters: login,
//               encoder: JSONParameterEncoder.default)
//    .response { response in
//      debugPrint(response)
//    }

    /// 2. 通过 HTTPHeader 数组构造

//    let headers: HTTPHeaders = [
//      HTTPHeader(name: "Authorization", value: "Basic VXNlcm5hbWU6UGFzc3dvcmQ="),
//      HTTPHeader(name: "Accept", value: "application/json")
//    ]
	  let headers: HTTPHeaders = ["Authorization":"Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
								 "Accept":"application/json"]

	  AF.request("https://httpbin.org/headers", headers: headers).responseDecodable(of: Login.self) { reponse in
		  
		  switch reponse.result {
			  case .success(let value):
				  print("请求成功,返回数据: \(value)")
			  case .failure(let error):
				  print("请求失败,错误信息: \(error)")
		  }
		  
	  }
  }


}
//https://wenku.csdn.net/column/22m833vis4


struct Login: Decodable {
  let email:String
  let password:String
}
